var path = require('path');
var fs = require('fs');

function NwForm() {
	var loadval = 0;
	var gui = require('nw.gui');

	var win = nw.Window.get();

	function getIPAdress() {
		var interfaces = require('os').networkInterfaces();
		for (var devName in interfaces) {
			var iface = interfaces[devName];
			for (var i = 0; i < iface.length; i++) {
				var alias = iface[i];
				if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
					return alias.address;
				}
			}
		}
		return '127.0.0.1';
	}
	var options = {
		"title": "Node Apps",
		"icon": "./public/logo/nw-icon.jpeg",
		"toolbar": false,
		"frame": false,
		"width": 1024,
		"height": 600,
		"fullscreen": true,
		"kiosk": false,
		"resizable": false,
		"show_in_taskbar": true,
		"show": true,
		"as_desktop": true,
		"position": "center",
		"min_width": 128,
		"min_height": 128,
		"always-on-top": false,
		"no-edit-menu": false,
	};
	var trayicon = './public/logo/tray-icon.png'
	if (process.platform == 'darwin') {
		trayicon = "./public/logo/osx-tray-icon.png"
		var your_menu = new nw.Menu({
			type: 'menubar'
		});
		var submenu = new nw.Menu();
		your_menu.append(new nw.MenuItem({
			label: 'First Menu',
			submenu: submenu
		}));
		win.menu = your_menu;
	} else if (process.platform == "win32" || process.platform == "win64") {
		trayicon = './public/logo/win-tray-icon.png';
	}
	var tray = new gui.Tray({
		icon: trayicon
	});
	var setmenu = function () {
		var self = menu.items[0];
		if (isShowWindow) {
			self.label = "Hide"
		} else {
			self.label = "Show"
		}
	}
	var menu = new nw.Menu();

	var isFullscreen = false;

	var isShowWindow = true;

	menu.append(new nw.MenuItem({
		click: function () {
			ShowHideForm()
		},
		label: 'Hide'
	}));
	menu.append(new nw.MenuItem({
		click: function () {

			if (isFullscreen) {
				win.leaveFullscreen()
				this.label = "Full screen";
				isFullscreen = false;
			} else {
				win.enterFullscreen();
				this.label = "Exit full screen"
				isFullscreen = true;
			}
		},
		label: 'Fullscreen'
	}));
	menu.append(new nw.MenuItem({
		click: function () {
			win.close(true);
		},
		enabled: true,
		label: 'Quit'
	}));

	tray.menu = menu;
	/*
	tray.on('click', function () {
		ShowHideForm()
	});
	*/
	win.on('show', function () {
		//console.log(111)
	});

	win.on('close', function () {
		ShowHideForm()
	});
	var ShowHideForm = function () {
		if (isShowWindow) {
			isShowWindow = false;
			win.hide();
		} else {
			isShowWindow = true;
			win.show();
		};
		setmenu()
	};

	this.ShowWebview = function (error, port, title) {
		if (error == null) {
			qekit_title = title;
			document.title = title;
			ShowWebform("http://" + getIPAdress() + ':' + port + "/admin/");
		} else {
			document.title = "error";

			qekit_title = "Error--189";
			ShowWebform("")
		}
	}
	var ShowWebform = function (src) {

		webform.src = src;
		var endtime = (new Date()).getTime();
		var time = endtime - starttime;
	  
		localStorage.setItem("interval", time);
		setval(100);
	}
	var self = this;
	process.FormName = "nw";
	var starttime = (new Date()).getTime();
	var www = require('./www');

	progressbar.setValue = function (val) {

		this.style.width = (val % 100) + "%"

	}
	var interval = 300000 / 96;
	if (localStorage.getItem("interval") != null) {
		var _interval = parseInt(localStorage.getItem("interval"));
		if (_interval > 0) {
			interval = _interval / 100;
		}
	}
	var qekit_title = "....................";
	var setval = function (_loadval) {

		if (interval == 0) {
			return;
		}
		loadval++;

		loadval = Math.min(96, loadval);
		if (_loadval) {
			loadval = _loadval
		}
		if (loadval < 100) {
			setTimeout(function () {
				setval()
			}, interval);
			var len = document.title.length;
			len++;
			len = Math.min(len, qekit_title.length);
			len = len >= qekit_title.length ? 1 : len;
			document.title = qekit_title.substring(0, len);
			progressbar.setValue(loadval)
		} else {
			progressbar.setValue(100);
			setTimeout(function () {
				interval = 0;
				document.title = qekit_title;
				document.body.className = "hideLoading";
			}, interval);
		}
	}
	setval();
	new www(require, process, process.cwd(), process.env.USERPROFILE ? process.env.USERPROFILE + "//" : process.env.HOME + "/", self);

};
new NwForm();